package com.artPreter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.artPreter.pojo.Depart;
import com.artPreter.mapper.DepartMapper;
import com.artPreter.pojo.User;
import com.artPreter.service.IDepartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 艺术机构表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2024-11-14
 */
@Service
public class DepartServiceImpl extends ServiceImpl<DepartMapper, Depart> implements IDepartService {

    /**
     * 艺术机构分页
     * @param currentPage
     * @param size
     * @param
     * @return
     */
    @Override
    public IPage<Depart> getPage(Integer currentPage, Integer size, Depart depart) {
        // 创建分页对象
        IPage<Depart> page = new Page<>(currentPage, size);
        // 构建查询条件
        LambdaQueryWrapper <Depart> queryWrapper = new LambdaQueryWrapper<>();
        // 排序
        if(depart.getIndexSort()!=null){
            queryWrapper.orderByDesc(Depart::getIndexSort);
        }else{
            queryWrapper.orderByDesc(Depart::getSort);
        }
        queryWrapper.eq(Depart::getStatus,1);

        // 机构名称模糊搜索
        if(depart.getName()!= null&&!"".equals(depart.getName())){
            queryWrapper.like(Depart::getName,depart.getName());
        }

        // 调用分页查询方法
        return this.baseMapper.selectPage(page, queryWrapper);
    }

    /**
     * 添加艺术机构
     * @param depart
     */
    @Override
    public void add(Depart depart) {
        String uuid = UUID.randomUUID().toString();
        depart.setDepartId(uuid);
        depart.setStatus(1);
        this.baseMapper.insert(depart);
    }

    /**
     * 修改艺术机构
     * @param depart
     */
    @Override
    public void edit(Depart depart) {
        this.baseMapper.updateById(depart);
    }

    /**
     * 删除艺术机构
     * @param depart
     */
    @Override
    public void delete(Depart depart) {
        depart.setStatus(0);
        this.baseMapper.updateById(depart);
    }

    @Override
    public Depart getInfo(Depart depart) {
        LambdaQueryWrapper<Depart> queryWrapper = new LambdaQueryWrapper<>();
        if(depart.getDepartId()!=null&&!"".equals(depart.getDepartId())){
            queryWrapper.eq(Depart::getDepartId,depart.getDepartId());
        }else if(depart.getId()!=null){
            queryWrapper.eq(Depart::getId,depart.getId());
        }else {
            return null;
        }
        List<Depart> list = this.baseMapper.selectList(queryWrapper);
        System.out.println("查询机构"+list.size()+list);

        if(list.size()==1){
            return list.get(0);
        }else{
            return null;
        }
    }
}
